package com.nazca.tvremote.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

import com.nazca.tvremote.R;
import com.nazca.tvremote.event.RemoteMotionEvent;
import com.nazca.tvremote.service.Controller;
import com.nazca.tvremote.util.RLog;
import com.nazca.tvremote.view.TitleBar;

public class MotionRemoteActivity extends Activity implements OnTouchListener {
    
    private static final String TAG = "MotionRemoteActivity";
    
    private static final int DEST_WIDTH = 800;
    private static final int HEIGHT_DEST = 480;
    private int mWidthTouchRang;
    private int mHeightTouchRang;
    private float mWidthRatio;
    private float mHeightRatio;
    
    private TitleBar mTitleBar;
    private ImageButton mButton;
    
    private View mTouchRange;
    
    private WakeLock mWakeLock = null;
    
    private float mStartX, mStartY;
    
    private Controller mController;
    
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.motion_remote);
        
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.getBackButton().setText(R.string.main_motion_remote);
        mTitleBar.getBackButton().setOnClickListener(new View.OnClickListener() {
            public void onClick (View v) {
                finish();
            }
        });
        
        mButton = (ImageButton) findViewById(R.id.setting);
        
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick (View v) {
                startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
            }
        });
        
        mTouchRange = findViewById(R.id.touch_range);
        mTouchRange.setOnTouchListener(this);
        
        Display display = getWindowManager().getDefaultDisplay();
        // RLog.d(TAG, "[display.width,display.height]:[" + display.getWidth() + "," +
        // display.getHeight() + "]");
        mWidthTouchRang = display.getWidth();
        mHeightTouchRang = 240;
        mWidthRatio = (float) DEST_WIDTH / (float) mWidthTouchRang;
        mHeightRatio = (float) HEIGHT_DEST / (float) mHeightTouchRang;
        
        mController = Controller.getInstance(getApplication());
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    }
    
    @Override
    protected void onResume () {
        super.onResume();
        acquireWakeLock();
        // Settings settings = Settings.getSettings(this);
    }
    
    @Override
    protected void onPause () {
        super.onPause();
        releaseWakeLock();
    }
    
    @Override
    public boolean onTouch (View v, MotionEvent event) {
        RLog.d(TAG, "onTouch()");
        RLog.d(TAG, "[x,y]:[" + event.getX() + "," + event.getY() + "]");
        // RLog.d(TAG, "[rawX,rawY]:[" + event.getRawX() + "," + event.getRawY() + "]");
        
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                mStartX = event.getX();
//                mStartY = event.getY();
//                break;
//            case MotionEvent.ACTION_UP:
//                mStartX = 0;
//                mStartY = 0;
//                break;
//            default:
//        }
        
        float dstX = event.getX() * mWidthRatio;
        float dstY = Math.min(event.getY() * mHeightRatio, HEIGHT_DEST);
        RLog.d(TAG, "[dstX,dstY]:[" + dstX + "," + dstY + "]");
        
        RemoteMotionEvent remoteEvent = new RemoteMotionEvent(event, dstX, dstY);
        mController.println(remoteEvent.toString());
        return true;
    }
    
    private void acquireWakeLock () {
        if (mWakeLock == null) {
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            // mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MAIL_SERVICE");
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass()
                .getCanonicalName());
            mWakeLock.acquire();
        }
    }
    
    private void releaseWakeLock () {
        if (mWakeLock != null && mWakeLock.isHeld()) {
            mWakeLock.release();
            mWakeLock = null;
        }
    }
}
